package com.ggh.jinjilive.ui.mall.collection.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemCollectionBinding;
import com.ggh.jinjilive.databinding.ItemShopPingCarAffirmBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.mall.collection.CollectionModule;
import com.ggh.jinjilive.mobile.mall.goods.GoodsShopSPMobile;
import com.ggh.jinjilive.mobile.mall.shopping.ShopMessageMoblie;
import com.ggh.jinjilive.ui.mall.goods.affirm.adaoter.ShopingCarAffirmGoodsAdapter;

import java.util.List;

public class CollectionAdapter extends RecyclerView.Adapter<CollectionAdapter.ItemViewHolder> {

    private List<CollectionModule> collectionModuleList;
    public void setAdd(List<CollectionModule> collectionModuleList) {
        this.collectionModuleList = collectionModuleList;
        notifyDataSetChanged();
    }

    private String management;
    public void setManagement(String management){
        this.management = management;
        notifyDataSetChanged();
    }



    /**
     * 更新数据
     */

    private AddItemOnClickListener addItemOnClickListener;

    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }


    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemCollectionBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_collection, parent, false);
        return new ItemViewHolder(binding);
    }


    public void onBindViewHolder(@NonNull ItemViewHolder holder, final int position) {
        CollectionModule collectionModule = collectionModuleList.get(position);
        if (null != collectionModule) {
            if(null != this.management && this.management.equals("管理")){
                holder.binding.cbCheckall.setVisibility(View.VISIBLE);
            }else {
                holder.binding.cbCheckall.setVisibility(View.GONE);
            }


            holder.binding.cbCheckall.setChecked(collectionModule.isSelected());
            holder.binding.cbCheckall.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(null != addItemOnClickListener){
                        if(collectionModule.isSelected()){
                            collectionModuleList.get(position).setSelected(false);
                        }else {
                            collectionModuleList.get(position).setSelected(true);
                        }
                        addItemOnClickListener.onSkipClicked(99999,collectionModuleList);
                    }
                }
            });
            //商品图片
            Glide.with(holder.binding.storeImg).load(collectionModule.getImg()).into(holder.binding.storeImg);
            //商品名称
            holder.binding.tvGoodsProductName.setText(collectionModule.getName());
            //
            holder.binding.tvSpecification.setText(collectionModule.getDescribes());
            //价格
            holder.binding.tvPrice.setText(collectionModule.getPrice() +"");

        }
    }


    @Override
    public int getItemCount() {

        return null == collectionModuleList ? 0 : collectionModuleList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemCollectionBinding binding;

        public ItemViewHolder(@NonNull ItemCollectionBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}